/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.capex.javaee.smtp;

import java.util.Properties;
import javax.mail.Authenticator;

import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.URLName;

/**
 *
 * @author Acer
 */
public class AllMessagesFromServer {

    public static void main(String[] args) throws Exception {
        URLName server = new URLName("protocol://username:password@host/foldername");

        Properties props = new Properties();

        String hostMailServer = "pop.gmail.com";
        String hostMailPort = "995";
        props.setProperty("mail.pop3.port", hostMailPort);
        props.put("mail.pop3.host", hostMailServer);
        props.put("mail.store.protocol", "pop3");
        props.put("mail.pop3.ssl.enable", "true");

        Session session = Session.getDefaultInstance(props, new MailAuthenticator());
        Store store = session.getStore();
        store.connect(hostMailServer, 995, "evegaluna@gmail.com", "k2i8k9o7");

        Folder folder = store.getDefaultFolder().getFolder("INBOX");
        if (folder == null) {
            System.out.println("Folder " + server.getFile() + " not found.");
            System.exit(1);
        }
        folder.open(Folder.READ_ONLY);

        // Get the messages from the server
        Message[] messages = folder.getMessages();
        for (int i = 0; i < messages.length; i++) {
            System.out.println("------------ Message " + (i + 1) + " ------------");
            messages[i].writeTo(System.out);
        }

        folder.close(false);
    }
}

//class MailAuthenticator extends Authenticator {
//
//    public MailAuthenticator() {
//    }
//
//    public PasswordAuthentication getPasswordAuthentication() {
//        return new PasswordAuthentication("evegaluna@gmail.com", "k2i8k9o7");
//    }
//}
